<script>
import AddAccountDialog from "@/components/comp/account_com/AddAccountDialog.vue";
import UpdateAccountDialog from "@/components/comp/account_com/UpdateAccountDialog.vue";

export default {
  name: "AcountList",
  components: {AddAccountDialog,UpdateAccountDialog},
  created(){
    //初始化表格数据
    this.init_table_data();
  },
  data() {
      return {
            tableData:[],
            page: {
              pageNow: 1,
              pageSize: 7,
              tj1: '',
              tj2: '',
              tj3:'',
              total: 0
            },
      }
  },
  methods:{
    //初始化表格数据
    async init_table_data(){
      const res =  await this.$http.get("accounts/get_accs", {params:this.page} );
      if(res.data.success){
        const pageInfo = res.data.data;
        this.tableData = pageInfo.records;
        this.page.total = pageInfo.total;
      }else{
        this.$message.error(res.data.errMsg);
      }
    },

    del_row(row){
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () =>  {
        //确认删除
         const b = await this.$http.get('accounts/del_by_id?id='+row.id);
        if(b){
          this.$message.success("恭喜，删除成功！");
          //刷新页面
          this.init_table_data();
        }else{
          this.$message.error("删除失败！！！");
        }
      }).catch(() => {
        this.$message.error("已取消删除！！！");
      });
    },



    sizeChange(val){
      this.page.pageSize = val;
      this.init_table_data();
    },

    pageChange(val){
      this.page.pageNow = val;
      this.init_table_data();
    },

    //性别显示格式化
    sexFormater(row){
      if(row.gender==0){
        return '男';
      }else {
        return '女';
      }
    },



    upQuestion(){
        this.$router.push('/upload_que')
    },

    show_add_dialog(){
        this.$refs.add_acc.show_dialog();
    },
    editor(row) {
      this.$refs.update_acc.show_dialog(row.id);
    },
    //修改、添加用户成功后，回调刷新
    option_ok_reload(){
        this.init_table_data();
    },

  }
}
</script>

<template>
  <div>
<!-------------------------------------------导航组件------------------------------------------------------------->
    <el-row>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>
<!-------------------------------------------搜索组件------------------------------------------------------------->
    <el-card class="box-card" style="margin-top: 20px">
      <el-row :gutter="20">
        <el-col :span="5" >
          <el-input @keyup.native.enter="init_table_data" v-model="page.tj1"  placeholder="请输入姓名、电话" >
            <i class="el-icon-search" slot="suffix"  style="line-height: 40px" @click="init_table_data"></i>
          </el-input>
        </el-col>

        <el-col :span="3" >
          <el-select v-model="page.tj2" placeholder="性别" clearable @change="init_table_data" >
            <el-option label="男" value="0"></el-option>
            <el-option label="女" value="1"></el-option>
          </el-select>
        </el-col>



<!--        <el-col :span="2">-->
<!--          <el-button @click="upQuestion" type="primary" >试题上传</el-button>-->
<!--        </el-col>-->



        <el-col :span="2" :offset="12">
          <el-button @click="show_add_dialog" type="primary" >添加用户</el-button>
        </el-col>
      </el-row>
<!-------------------------------------------表格组件------------------------------------------------------------->
      <el-row style="margin-top: 20px">

        <el-table :data="tableData" ref="accountTable" border style="width: 100%">

          <el-table-column
              type="index"
              align="center"
              width="80">
          </el-table-column>

          <el-table-column
              prop="realName"
              align="center"
              label="姓名"
              width="120">
          </el-table-column>

          <el-table-column
              prop="createTime"
              label="注册时间"
              align="center"
              width="120">
            <template slot-scope="scope">
              {{String(scope.row.createTime).substr(0,10)}}
            </template>
          </el-table-column>

          <el-table-column
              prop="gender"
              label="性别"
              align="center"
              width="120"
              :formatter="sexFormater"
          >
          </el-table-column>


          <el-table-column
              prop="age"
              width="120"
              align="center"
              label="年龄">
            <template slot-scope="scope">
              {{scope.row.age}}岁
            </template>
          </el-table-column>


          <el-table-column
              prop="qualification"
              label="学历"
              align="center"
              width="120">
          </el-table-column>

          <el-table-column
              prop="phone"
              label="电话"
              align="center"
              width="180">
          </el-table-column>



          <el-table-column
              prop="address"
              align="center"
              label="地址">
            <template slot-scope="scope">
              <el-popover trigger="hover" placement="top">
                <p>住址: {{ scope.row.address }}</p>
                <div slot="reference" class="name-wrapper">
                  <el-tag size="medium" >{{ String(scope.row.address).substr(0,11) }}</el-tag>
                </div>
              </el-popover>
            </template>
          </el-table-column>




          <el-table-column
              fixed="right"
              label="操作"
              width="100">
            <template slot-scope="scope">
              <el-button @click="editor(scope.row)" type="primary" size="small" icon="el-icon-edit" circle></el-button>
              <el-button @click="del_row(scope.row)" type="danger" icon="el-icon-delete" size="small" circle></el-button>
            </template>
          </el-table-column>
        </el-table>

<!-------------------------------------------分页组件------------------------------------------------------------->
        <el-pagination
            @current-change="pageChange"
            @size-change="sizeChange"
            :current-page="page.pageNow"
            :page-sizes="[7, 15, 25, 50]"
            :page-size="page.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            style="margin-top: 20px;float:right"
            :total="page.total">
        </el-pagination>
      </el-row>
    </el-card>

<!-------------------------------------------用户列表子组件------------------------------------------------------------->
    <AddAccountDialog ref="add_acc" @add_ok="option_ok_reload"></AddAccountDialog>
    <UpdateAccountDialog ref="update_acc" @updated_user="option_ok_reload"></UpdateAccountDialog>
  </div>
</template>

<style scoped>

</style>